package com.cnhis.cloudhealth.clidoctorweb.solr;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import org.jboss.resteasy.logging.Logger;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Reference;
import com.cnhis.cloudhealth.clinical.clidoctor.lucence.service.ILucenceService;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.interceptor.SpringContextHolder;
import com.cnhis.cloudhealth.commons.rediscache.CookieUtil;
import com.cnhis.cloudhealth.commons.redisdb.RedisResource;
import com.cnhis.cloudhealth.commons.utils.JsonUtil;

@Path("solr")
@Component
public class SolrRest
{
    Logger log = Logger.getLogger(SolrRest.class);
    
    @Reference
    private ILucenceService lucenceService;
    private RedisResource redisResource = (RedisResource) SpringContextHolder.getBean("redisResource");
    
    /**
     * 批量更新医嘱索引
     * @return
     * @throws Exception
     */
    @GET
    @Path("updateAdvice")
    @Produces({MediaType.TEXT_PLAIN})
    public ModelVo updateAdvice(@QueryParam("params") String params)
        throws Exception
    {
        Map<String,Object> map = JsonUtil.getMap(params);
        ModelVo mv = lucenceService.updateAdvice(map);
        return mv;
    }
    
    /**
     * 按项目类型批量更新医嘱索引
     * @return
     * @throws Exception
     */
    @GET
    @Path("updateAdviceByBda01")
    @Produces({MediaType.TEXT_PLAIN})
    public ModelVo updateAdviceByBda01(@QueryParam("params") String params)
        throws Exception
    {
        Map<String,Object> map = JsonUtil.getMap(params);
        ModelVo mv = lucenceService.updateAdviceByBda01(map);
        return mv;
    }
    
    
    /**
     * 批量删除医嘱索引
     * @return
     * @throws Exception
     */
    @GET
    @Path("deleteAdvice")
    @Produces({MediaType.TEXT_PLAIN})
    public ModelVo deleteAdvice(@QueryParam("params") String params)
        throws Exception
    {
        Map<String,Object> map = JsonUtil.getMap(params);
        return lucenceService.deleteAdvice(map);
    }
    
    
    /**
     * 初始化药品、检验、检查、手术、治疗、材料solr索引
     * @return
     * @throws Exception
     */
    @GET
    @Path("initAdviceIndex")
    @Produces({MediaType.APPLICATION_JSON})
    @SuppressWarnings({"unchecked", "rawtypes"})
    public Map initAdviceIndex(@QueryParam("params") String params,@Context HttpServletRequest request)
    {
        HashMap map = new HashMap();
        Map temp = JsonUtil.getMap(params);
        try
        {
            String dbtype = "";
            String sid = CookieUtil.getCookieValue(request, "jsessionids");
            dbtype = (String)redisResource.opsForValueGet("db" + sid);
            temp.put("corporationid", dbtype+temp.get("yiyuanId"));
            temp.put("yiyuanid", temp.get("yiyuanId"));
            temp.put("db_corporationid", dbtype+"\\"+temp.get("yiyuanId"));
//            log.info(GlobalVariable.StringIsNull("corporationid======================"+temp.get("corporationid")));
            lucenceService.initIndex(temp);
            map.put("result", "Success");
        }
        catch (Exception e)
        {
            e.printStackTrace();
            map.put("result", "Failure");
        }
        return map;
    }
    
    /**
     * 初始化 门诊医生系统solr索引库
     * 根据dbtype和yiyuanid初始化solr数据
     * @return
     * @throws Exception
     */
    @GET
    @Path("initClinicAdviceSolr")
    @Produces({MediaType.APPLICATION_JSON})
    @SuppressWarnings({"rawtypes"})
    public ModelVo initClinicAdviceSolr(@QueryParam("params") String params,@Context HttpServletRequest request) throws Exception
    {
        Map temp = JsonUtil.getMap(params);
        return lucenceService.initClinicAdviceSolr(temp);
    }
    


    /**
     * 初始化药品参考索引文件
     * @return
     * @throws Exception
     */
    @GET
    @Path("initYaopincankaoIndex")
    @Produces({MediaType.APPLICATION_JSON})
    @SuppressWarnings({"unchecked", "rawtypes"})
    public Map initYaopincankaoIndex()
    {
        HashMap map = new HashMap();
        try
        {
            map.put("result", "Success");
            lucenceService.initYaopincankaoIndex();
        }
        catch (Exception e)
        {
            map.put("result", "Failure");
            e.printStackTrace();
        }
        return map;
    }
    
    /**
     * 初始化诊断索引文件
     * @return
     * @throws Exception
     */
    @GET
    @Path("initzhenduanIndex")
    @Produces({MediaType.APPLICATION_JSON})
    @SuppressWarnings({"unchecked", "rawtypes"})
    public Map initzhenduanIndex(@QueryParam("params") String params,@Context HttpServletRequest request)
    {
        HashMap map = new HashMap();
        Map temp = JsonUtil.getMap(params);
        try
        {
            String dbtype = "";
            String sid = CookieUtil.getCookieValue(request, "jsessionids");
            dbtype = (String)redisResource.opsForValueGet("db" + sid);
            temp.put("corporationid", dbtype+temp.get("corporationid"));
            temp.put("db_corporationid", dbtype+"\\"+temp.get("corporationid"));
//            log.info(GlobalVariable.StringIsNull("corporationid======================"+temp.get("corporationid")));
            lucenceService.initzhenduanIndex(temp);
            map.put("result", "Success");
        }
        catch (Exception e)
        {
            map.put("result", "Failure");
            e.printStackTrace();
        }
        return map;
    }
    
    /**
     * 初始化医保诊断索引文件
     * @return
     * @throws Exception
     */
    @GET
    @Path("inityibaozhenduanIndex")
    @Produces({MediaType.APPLICATION_JSON})
    @SuppressWarnings({"unchecked", "rawtypes"})
    public Map inityibaozhenduanIndex(@QueryParam("params") String params,@Context HttpServletRequest request)
    {
        HashMap map = new HashMap();
        Map temp = JsonUtil.getMap(params);
        try
        {   
            String dbtype = "";
            String sid = CookieUtil.getCookieValue(request, "jsessionids");
            dbtype = (String)redisResource.opsForValueGet("db" + sid);
            temp.put("corporationid", dbtype+temp.get("corporationid"));
            temp.put("db_corporationid", dbtype+"\\"+temp.get("corporationid"));
//            log.info(GlobalVariable.StringIsNull("corporationid======================"+temp.get("corporationid")));
            lucenceService.inityibaozhenduanIndex(temp);
            map.put("result", "Success");
        }
        catch (Exception e)
        {
            map.put("result", "Failure");
            e.printStackTrace();
        }
        return map;
    }
    
    
    /**
     * 医嘱智能搜索
     * @return
     * @throws Exception
     */
    @POST
    @Path("queryAdviceBySolr")
    @Produces({MediaType.APPLICATION_JSON})
    public ModelVo queryAdviceBySolr(@FormParam("params") String params,@Context HttpServletRequest request) throws Exception
    {
        Map<String, Object> paramsObj = JsonUtil.getMap(params);
        String dbtype = "";
        String sid = CookieUtil.getCookieValue(request, "jsessionids");
        dbtype = (String)redisResource.opsForValueGet("db" + sid);
        paramsObj.put("q", paramsObj.get("q")+" AND corporationid:"+dbtype+"\\"+paramsObj.get("yiyuanId"));
//        log.info("q======================="+paramsObj.get("q"));
        return lucenceService.queryAdviceBySolr(paramsObj);
    }
    
    
}
